<%@ page contentType="text/html;charset=UTF-8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<%@ include file="/common/meta.jsp"%>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>SpringMVC+Hibernate +MySql+ EasyUI ---CRUD</title>
<script type="text/javascript">
	var searchString;

	function resizeDg(){
		$('#dg').datagrid("resize", { width: $(window).width() * 0.5});
	}
	
	function getCookie(c_name){
	     if (document.cookie.length>0){
		  c_start=document.cookie.indexOf(c_name + "=");
		  if (c_start!=-1){ 
		    c_start=c_start + c_name.length+1;
		    c_end=document.cookie.indexOf(";",c_start);
		    if (c_end==-1) {
		    	c_end=document.cookie.length;
		    }
		    return document.cookie.substring(c_start,c_end);
		   } 
		  }
		return "";
	}
	
	var pageSize = 20;
	var pageNumber = 1;
	var sortName = '';
	var sortOrder = '';
	function initDate(){
		var s = getCookie("role");
		s = decodeURIComponent(s);
		if(s != null && s != ""){
			searchMap = eval('(' + s + ')');
			pageSize = searchMap.rows;
			if(pageSize == null || pageSize == ""){
				pageSize = 20;
			}
			pageNumber = searchMap.pageNumber;
			sortName = searchMap.sortName;
			sortOrder = searchMap.sortOrder;
			$("#name").val(searchMap.name );
		}
	}
	
	$(function(){
		 $("#doSearch").click(function(){
			doSearch();
		});
		initDate();
		var name=$("#name").val();
		$('#dg').datagrid({
		    url:"${ctx }/user/list",
			pagination:true,
			singleSelect:false,
			rownumbers:true,
		    pageSize:pageSize,
		    pageNumber:pageNumber,
		    sortOrder:sortOrder,
		    sortName:sortName,
		    queryParams:{  
		        name:name,
		    },
		    width:800,
		   	columns:[[
		   		{field:'id',title:'名称', width:100, align:"center",sortable:true,checkbox:true},
		   		{field:'name',title:'名称', width:100, align:"center",sortable:true},
		   		{field:'age',title:'年龄', width:50, align:"center",sortable:true},
		   		{field:'birthday',title:'生日', width:100, align:"center",sortable:true},
		   		{field:'address',title:'地址', width:50, align:"center",sortable:true},
		   		{field:'password',title:'学号', width:50, align:"center",sortable:true},
		   		{field:'operation',title:'操作', width:320, align:"center", sortable:false,
		   			formatter:function(value,row,index){
		   				var s ="";
		                s+="<a href=\"javascript:void(0)\"><span onclick=\"javaScript:gotoModify('"+row.id+"');\">修改</span></a>";
               			s += "|";
		                s+="<a href=\"javascript:void(0)\"><span onclick=\"javaScript:gotoDel('"+row.id+"');\">删除</span>&nbsp;&nbsp;</a>";
			            return s;
		   			}
		   		}
		   	]],
		   	/*导出excel 按钮*/
		   	toolbar:[{
		   	id:'btnsave',
					text:'导出',
					iconCls:'icon-save',
					handler:function(){
						var url = '${ctx}/export?method=sheetsExport';
						window.location.href=url;
					}
		   	}]
		});
		 var p = $('#dg').datagrid('getPager');    
         $(p).pagination({    
              pageList: [10,20,50,100]
          });  
		
		$("#doSearch").click(function(){
			doSearch();
		});
	});
	
	
	function gotoAdd(){
		var url = '${ctx }/user/gotoAdd';
		window.location.href=url;
	}
	function gotoModify(id){
		var url = '${ctx}/user/gotoModify?id='+id;
		window.location.href=url;
	}
	function gotoDel(id){
		if(!confirm('确定删除所选记录？')){
			return;
		}
		var url = '${ctx}/user/delete?id='+id;
		$.ajax({
			type : 'post',
			url : url,
			dataType: "json",
    			success:function(data){
					if(data.success == true){
						doSearch();
					}else{
						alert(data.msg);
					}
				}
			});
	}
		
	/****批量删除******/
	$(function(){
	$("#deletes").bind("click",function(){
		var selrow = $("#dg").datagrid("getSelections");
		if(selrow.length==0){
		alert("至少选择一条数据");
		return false;
		}
		var ids=[];
		for(var i = 0; i < selrow.length; i++){
		//	var name = selrow[i].name;
		//	if(typeof (name) == "undefined"){
		//	alert("");
		//	return false;
		//	}
			var id = selrow[i].id;
			ids.push(id);
		}
		if(confirm("您确定批量删除")){
			$.getJSON("${ctx}/user/delall",{"array[]":ids},
			function(data){
				alert(data.msg);
				if(true == data.success){
					$("#dg").datagrid("reload");
				}
			})
		}
	})
	})
	/*--导入数据--*/
	/*$(function(){
		$("#upload_excel").resetForm().validate({
			rules:{
				"import_fileup":{
					required:true;
					accept:"xls";
				},
				message:{
				"import_fileup":{
					require:"请选择文件",
					accept:"error please upload .xls file"
					}
				},
				submitHandler:function(){
					$("#upload_excel").ajaxSubmit({
						url:"${ctx}/",
						type:'post',
						enctype:"multipart/form-data",  
                        contentType: "application/x-www-form-urlencoded; charset=utf-8", 
                        dataType:'json',
                        success:function(data){
                                                   var str = "";  
                            for (var i=0, len=data.length; i < len; i++) {  
                                str += "<p>";  
                                str += data[i]["deparement"] + "," + data[i]["name"] + "," + data[i]["date"];  
                                str += "</p>";  
                            }  
                            $("#jsonBtn").removeAttr("disabled").removeAttr("title");  
                        },  
                        error: function() {  
                            alert('error');  
                        }  
					})
				}
			}
		})
	})
	*/
	function doSearch(){
		var name=$("#name").val();
		/* var schoolId=$("#schoolId").val(); */
		$("#dg").datagrid('load',{  
	        name:name
	    }); //重新载入 
	}
	function Exproter(){
		var rows = $('#dg').datagrid("getRows");
		var oXl = new ActiveXObject("Excel.Application");
		var oWb = oXl.Workbooks.Add();
		var oSheet = oWb.AciveSheet;
		for(var i = 0; i < rows.length; i++){
			oSheet.Cells(i + 1,1).value = rows[i].O_NAME;
		}
		oXl.Visible = true;
	}	
</script>
</head>
<body onload="resizeDg();" onresize="resizeDg();" ><center>
<div class="neirong">
<div class="add-content" style="margin-top:0">
	<div class="xinxi2">
       	<div class="search_box">
           <p>名称： <input name="name" id="name" type="text" /></p>
           <a href="javascript:void(0);" id="doSearch" class="blank_btn">查询</a></div>
           <div class="btn_div">
           <a href="javascript:void(0);" onclick="gotoAdd();" id="xtsz_rygl_jsgl_add" class="blank_btn">新增</a>
           <a href="javascript:void(0);" onclick="Exproter();" id="exproter" class="blank_btn">导出</a>
           <a href="javascript:void(0);" id="deletes" class="blank_btn" >批量删除</a>
           	<form enctype="multipart/form-data" method="post" id="upload_excel">
         <!--   <input type="file" name="import_fileup" id="import_fileup" title="导入数据"></input>
           <input type="submit" name="import_file" id="import_file" title="上传数据"></input>  -->
           </form>
           <!-- <a href="javascript:void(0);" onclick="gotoExcel();" id="xtsz_rygl_jsgl_add" class="blank_btn">导出</a> -->
           </div>
       </div>
	<div class="contant_list" >
		<!-- c_top start-->
		<table  width="100%">
			<tr>
				<td>
					<table id="dg">
						
					</table>
				</td>
			</tr>
		</table>
	</div>
  </div>
</div>
</center>
</body>
</html>